﻿namespace OleDbDAL.Stock
{
    using DBUtility;
    using IDal.Stock;
    using Model.Stock;
    using System;
    using System.Data;
    using System.Text;

    public class DigitallibraryApp : IDigitallibrary
    {
        private string dbname = "Systems";

        public void Add(DigitallibraryEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_Digitallibrary(");
            builder.Append("ChainNum,DlNum,DlName,WarehouseStaffMan,LastCheckTime,Remark,CreateMan,CreateTime,IsVtag");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("'" + model.DlNum + "',");
            builder.Append("'" + model.DlName + "',");
            builder.Append("" + model.WarehouseStaffMan + ",");
            builder.Append("'" + model.LastCheckTime + "',");
            builder.Append("'" + model.Remark + "',");
            builder.Append("" + model.CreateMan + ",");
            builder.Append("'" + model.CreateTime + "',");
            builder.Append("" + model.IsVtag + "");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int DlId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_Digitallibrary ");
            builder.Append(" where DlId=" + DlId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Digitallibrary ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by DlId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "DlId", "T_Digitallibrary", swhere);
        }

        public DigitallibraryEnt GetModel(int DlId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select DlId,ChainNum,DlNum,DlName,WarehouseStaffMan,LastCheckTime,Remark,CreateMan,CreateTime,IsVtag from T_Digitallibrary ");
            builder.Append(" where DlId=" + DlId);
            DigitallibraryEnt ent = new DigitallibraryEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.DlId = DlId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.DlNum = table.Rows[0]["DlNum"].ToString();
                ent.DlName = table.Rows[0]["DlName"].ToString();
                if (table.Rows[0]["WarehouseStaffMan"].ToString() != "")
                {
                    ent.WarehouseStaffMan = int.Parse(table.Rows[0]["WarehouseStaffMan"].ToString());
                }
                if (table.Rows[0]["LastCheckTime"].ToString() != "")
                {
                    ent.LastCheckTime = DateTime.Parse(table.Rows[0]["LastCheckTime"].ToString());
                }
                ent.Remark = table.Rows[0]["Remark"].ToString();
                if (table.Rows[0]["CreateMan"].ToString() != "")
                {
                    ent.CreateMan = int.Parse(table.Rows[0]["CreateMan"].ToString());
                }
                if (table.Rows[0]["CreateTime"].ToString() != "")
                {
                    ent.CreateTime = DateTime.Parse(table.Rows[0]["CreateTime"].ToString());
                }
                if (table.Rows[0]["IsVtag"].ToString() != "")
                {
                    ent.IsVtag = int.Parse(table.Rows[0]["IsVtag"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Digitallibrary ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "DlId");
        }

        public void Update(DigitallibraryEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_Digitallibrary set ");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("DlNum='" + model.DlNum + "',");
            builder.Append("DlName='" + model.DlName + "',");
            builder.Append("WarehouseStaffMan=" + model.WarehouseStaffMan + ",");
            builder.Append("LastCheckTime='" + model.LastCheckTime + "',");
            builder.Append("Remark='" + model.Remark + "',");
            builder.Append("CreateMan=" + model.CreateMan + ",");
            builder.Append("CreateTime='" + model.CreateTime + "',");
            builder.Append("IsVtag=" + model.IsVtag + "");
            builder.Append(" where DlId=" + model.DlId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

